package com.example.parkdemo.parking

import android.content.DialogInterface
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.base.user.ParkingResult
import com.example.base.user.ParkingResultItem
import com.example.parkdemo.R
import com.example.parkdemo.adapter.ParkingAdapter
import com.example.parkdemo.databinding.ActivityParkingBinding
import com.example.parkdemo.parking.vr.ParkingVRActivity
import com.example.parkdemo.ping.PagingAdapter
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class ParkingActivity : BaseActivity<ActivityParkingBinding, ParkingViewModel>() {
    lateinit var parkingAdapter: ParkingAdapter
    lateinit var parkingResultItem: ParkingResultItem
    override fun initData() {
        vm.getPark()
        vm.parkingSuccess.observe(this) {
            val list = Gson().fromJson(it, ParkingResult::class.java)
            parkingAdapter.data.clear()
            parkingAdapter.data.addAll(list)
            parkingAdapter.notifyDataSetChanged()
        }
        vm.delParkingSuccess.observe(this) {
            ToastUtils.showLong("删除成功")
            parkingAdapter.remove(parkingResultItem)
        }
    }

    override fun initView() {
        binding.parkingHeader.headerBack.setOnClickListener { finish() }
        binding.parkingHeader.headerTitle.text = "车位申请"

        parkingAdapter = ParkingAdapter()
        binding.rv.adapter = parkingAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)


        parkingAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("删除车位？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : DialogInterface.OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        parkingResultItem = parkingAdapter.data[position]
                        val map = mutableMapOf<String, Int>()
                        map[Const.PARAM_PARKING_ID] = parkingAdapter.data[position].parkId
                        val json = Gson().toJson(map)
                        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                        vm.getDelParking(body)
                    }
                })
                .show()
            true
        }
        parkingAdapter.setOnItemClickListener { adapter, view, position ->
            parkingResultItem = parkingAdapter.data[position]
            val intent = Intent(this, ParkingVRActivity::class.java)
            intent.putExtra(Const.PARAM_PARKING_BEAN, parkingResultItem)
            startActivity(intent)
        }


    }

}